package com.basin.common.mp.handler;

import com.basin.common.core.exception.BasinException;
import com.basin.common.core.response.ResponseResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.sql.SQLException;

/**
 * @author: Sam ZHONG | sammy1997@aliyun.com
 */
@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {
    @ExceptionHandler(value = {BasinException.class})
    public ResponseResult<String> handleBasinException(BasinException e) {
        log.error("全局异常处理器-BasinException：{}", e.getMessage(), e);
        return ResponseResult.fail(e.getCode(), e.getMessage());
    }

    @ExceptionHandler(value = {SQLException.class})
    public ResponseResult<String> handleSqlException(SQLException e) {
        log.error("全局异常处理器-SQLException：{}", e.getMessage(), e);
        return ResponseResult.fail(500, "SQL错误 {" + e.getMessage() + "}");
    }

    @ExceptionHandler(value = {Exception.class})
    public ResponseResult<String> handleException(Exception e) {
        log.error("全局异常处理器-Exception：{}", e.getMessage(), e);
        return ResponseResult.fail(500, "系统错误 {" + e.getMessage() + "}");
    }
}
